<?php

// +----------------------------------------------------------------------

// | Fanwe 多语商城建站系统 (Build on ThinkPHP)

// +----------------------------------------------------------------------

// | Copyright (c) 2009 http://www.fanwe.com All rights reserved.

// +----------------------------------------------------------------------

// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )

// +----------------------------------------------------------------------

// | Author: 云淡风轻(97139915@qq.com)

// +----------------------------------------------------------------------



// 留言评论

class MessageAction extends CommonAction{

	public function typeUcMessage()

	{

		//列表过滤器，生成查询Map对象

		$map = $this->_search ();

		$map['pid'] = intval($_REQUEST['msg_id']);

		$map['rec_module'] = 'UcMessage';

		$map['reply_type'] = intval($_REQUEST['reply_type']);

		if (method_exists ( $this, '_filter' )) {

			$this->_filter ( $map );

		}

		$name=$this->getActionName();

		$model = D ($name);

		if (! empty ( $model )) {

			$this->_list ( $model, $map );

		}

		$this->display();

		return;

	}

	

	public function typeMessage()

	{

		//列表过滤器，生成查询Map对象

		$map = $this->_search ();

		$map['pid'] = intval($_REQUEST['msg_id']);

		$map['rec_module'] = 'Message';

		$map['reply_type'] = intval($_REQUEST['reply_type']);

		if (method_exists ( $this, '_filter' )) {

			$this->_filter ( $map );

		}

		$name=$this->getActionName();

		$model = D ($name);

		if (! empty ( $model )) {

			$this->_list ( $model, $map );

		}

		$this->display();

		return;

	}

	

	

	public function typeFeedback()

	{

		//列表过滤器，生成查询Map对象

		$map = $this->_search ();

		$map['pid'] = intval($_REQUEST['msg_id']);

		$map['rec_module'] = 'Feedback';

		$map['reply_type'] = intval($_REQUEST['reply_type']);

		if (method_exists ( $this, '_filter' )) {

			$this->_filter ( $map );

		}

		$name=$this->getActionName();

		$model = D ($name);

		if (! empty ( $model )) {

			$this->_list ( $model, $map );

		}

		$this->display();

		return;

	}

	

	public function typeLinks()

	{

		//列表过滤器，生成查询Map对象

		$map = $this->_search ();

		$map['pid'] = intval($_REQUEST['msg_id']);

		$map['rec_module'] = 'Links';

		$map['reply_type'] = intval($_REQUEST['reply_type']);

		if (method_exists ( $this, '_filter' )) {

			$this->_filter ( $map );

		}

		$name=$this->getActionName();

		$model = D ($name);

		if (! empty ( $model )) {

			$this->_list ( $model, $map );

		}

		$this->display();

		return;

	}

	

	public function typeSite()

	{

		//列表过滤器，生成查询Map对象

		$map = $this->_search ();

		$map['pid'] = intval($_REQUEST['msg_id']);

		$map['rec_module'] = 'Site';

		$map['reply_type'] = intval($_REQUEST['reply_type']);

		if (method_exists ( $this, '_filter' )) {

			$this->_filter ( $map );

		}

		$name=$this->getActionName();

		$model = D ($name);

		if (! empty ( $model )) {

			$this->_list ( $model, $map );

		}

		$this->display();

		return;

	}

	

	public function typeGoodsMessage()

	{

		$title = trim($_REQUEST['title']);

		$goods_name = trim($_REQUEST['goods_name']);

		$user_name = trim($_REQUEST['user_name']);

		

		if(!empty($title))

		{

			$this->assign("title",$title);

			$where.=" and m.title like '%$title%'";

			$parameter .= "title=" . urlencode($title)."&";

		}

		

		if(!empty($goods_name))

		{

			$this->assign("goods_name",$goods_name);

			$where.=" and g.name like '%$goods_name%'";

			$parameter .= "goods_name=" . urlencode($goods_name)."&";

		}

		

		if(!empty($user_name))

		{

			$this->assign("user_name",$user_name);

			$where.=" and m.user_name like '%$user_name%'";

			$parameter .= "user_name=" . urlencode($user_name)."&";

		}

		$sql_str = 'SELECT m.*,g.name as goods_name '.

					'FROM '.C("DB_PREFIX").'message as m '.

					'left join '.C("DB_PREFIX").'goods as g on g.id = m.rec_id '.

					"where m.rec_module = 'Goods' $where group by m.id";

		$model = M();

        $voList = $this->_Sql_list($model, $sql_str, "&".$parameter, 'id', false);

		

		$this->display ();

		return;

	}

	

	public function typeSiteMessage()

	{

		$title = trim($_REQUEST['title']);

		$site_name = trim($_REQUEST['site_name']);

		$user_name = trim($_REQUEST['user_name']);

		

		if(!empty($title))

		{

			$this->assign("title",$title);

			$where.=" and m.title like '%$title%'";

			$parameter .= "title=" . urlencode($title)."&";

		}

		

		if(!empty($site_name))

		{

			$this->assign("site_name",$site_name);

			$where.=" and s.name like '%$site_name%'";

			$parameter .= "site_name=" . urlencode($site_name)."&";

		}

		

		if(!empty($user_name))

		{

			$this->assign("user_name",$user_name);

			$where.=" and m.user_name like '%$user_name%'";

			$parameter .= "user_name=" . urlencode($user_name)."&";

		}

		

		$sql_str = 'SELECT m.*,s.name as site_name '.

					'FROM '.C("DB_PREFIX").'message as m '.

					'left join '.C("DB_PREFIX").'site as s on s.id = m.rec_id '.

					"where m.rec_module = 'SiteMessage' $where group by m.id";

		

		$model = M();

        $voList = $this->_Sql_list($model, $sql_str, "&".$parameter, 'id', false);

		

		$this->display ();

		return;

	}

	

	public function typeInfoMessage()

	{

		$title = trim($_REQUEST['title']);

		$info_title = trim($_REQUEST['info_title']);

		$user_name = trim($_REQUEST['user_name']);

		

		if(!empty($title))

		{

			$this->assign("title",$title);

			$where.=" and m.title like '%$title%'";

			$parameter .= "title=" . urlencode($title)."&";

		}

		

		if(!empty($info_title))

		{

			$this->assign("info_title",$info_title);

			$where.=" and ci.title like '%$info_title%'";

			$parameter .= "info_title=" . urlencode($info_title)."&";

		}

		

		if(!empty($user_name))

		{

			$this->assign("user_name",$user_name);

			$where.=" and m.user_name like '%$user_name%'";

			$parameter .= "user_name=" . urlencode($user_name)."&";

		}

		

		$sql_str = 'SELECT m.*,ci.title as info_title '.

					'FROM '.C("DB_PREFIX").'message as m '.

					'left join '.C("DB_PREFIX").'city_info as ci on ci.id = m.rec_id '.

					"where m.rec_module = 'CityInfo' $where group by m.id";

		

		$model = M();

        $voList = $this->_Sql_list($model, $sql_str, "&".$parameter, 'id', false);

		

		$this->display ();

		return;

	}

		

	//查看与回复

	function edit() {

		$name=$this->getActionName();

		$model = M ( $name );

		$id = $_REQUEST [$model->getPk ()];

		$this->assign("param",array('msg_id'=>$id,'reply_type'=>1));  //输出查看回复列表的参数		

		$vo = $model->getById ( $id );

		$this->assign("param_back",array('msg_id'=>$vo['pid'],'reply_type'=>$vo['pid']>0?1:0));  //输出跳回回复列表页的参数

		$this->assign("rec_param",array("id"=>$vo['rec_id']));  //输出关联数据的参数

		$vo['user_info'] = D("User")->getById($vo['user_id']);

		$rec_module = $vo['rec_module'];  //当前留言类型

		//赋值管理员的回复

		

		if($rec_module == "Site")

		{

			$siteCates = D("SiteCate")->where("status = 1")->order("sort asc,id desc")->findAll();

			$this->assign ('siteCates', $siteCates );

			

			$ruleType = D("CollectRuleType")->where("status = 1")->order("sort asc,id desc")->findAll();

			$this->assign("ruleType",$ruleType);

		}

		

		$rep = D("Message")->where("pid=".$vo['id']." and reply_type=2")->find();

		$vo['adm_title'] = $rep['title'];

		$vo['adm_content'] =  $rep['content'];

		$this->assign ('vo', $vo );	

		$this->assign('back_action','type'.$rec_module);	

		

		$this->display ();

	}

	

	public function swTopStatus() {

			$name=$this->getActionName();

			$status = $_REQUEST['status'];

			$model = D ($name);

			$pk = $model->getPk ();

			$id = $_REQUEST [$pk];

			$item = $model->getById($id);

			if($status)

			{

				$list = $model->where($pk."=".$id)->setField('is_top',0);

			}

			else 

			{

				$list = $model->where($pk."=".$id)->setField('is_top',1);

			}

			if ($list!==false) {	

				if($status)

				{		

					$msg = '取消置顶留言:'.$item['title'];

				}

				else

				{

					$msg = '置顶留言:'.$item['title'];	

				}

				$this->saveLog(1,$item['id'],$msg);

				$this->assign ( "jumpUrl", $this->getReturnUrl () );

				$this->success ( L('FORBID_SUCCESS') );

			} else {

				$msg = '置顶留言:'.$item['title'];

				$this->saveLog(0,$item['id'],$msg);

				$this->error  (  L('FORBID_FAILED') );

			}

		}

			

	function update() {

		//B('FilterString');

		$id = intval($_REQUEST['id']);

		$name=$this->getActionName();

		$model = D ( $name );

		if (false === $data = $model->create ()) {

			$this->error ( $model->getError () );

		}

		// 更新数据

		$data['title'] = htmlspecialchars($data['title'],ENT_QUOTES);

		$data['content'] = htmlspecialchars($data['content'],ENT_QUOTES);

		$list=$model->save ($data);

		

		if (false !== $list) {

			$msgData = D("Message")->getById($id);  //当前留言的信息

			//开始更新管理员的相应回复

			$adm_reply = D("Message")->where("pid=".$id." and reply_type=2")->find();

			if($adm_reply)

			{

				//已有回复的修改

				if($_REQUEST['adm_title']==""&&$_REQUEST['adm_content']==""||$_REQUEST['adm_title']==""&&$_REQUEST['adm_content']=="<br />")

				{

					//删除管理员回复

					D("Message")->where("pid=".$id." and reply_type=2")->delete();					

				}

				else 

				{

					$adm_reply['title'] = $_REQUEST['adm_title'];

					$adm_reply['content'] = $_REQUEST['adm_content'];

					$adm_reply['update_time'] = gmtTime();

					$adm_reply['score'] = 0;

					D("Message")->save($adm_reply);  //更新回复

				}

			}

			else 

			{

				//新增管理员回复

				if($_REQUEST['adm_title']!="" || $_REQUEST['adm_content']!="")

				{

					if($_REQUEST['adm_title'] == "")

						$_REQUEST['adm_title'] = $_REQUEST['adm_content'];

					

					$adm_reply['title'] = $_REQUEST['adm_title'];

					$adm_reply['content'] = $_REQUEST['adm_content'];

					$adm_reply['create_time'] = gmtTime();

					$adm_reply['update_time'] = gmtTime();

					$adm_reply['pid'] = $id;

					$adm_reply['reply_type'] = 2;

					$adm_reply['status'] = 1;

					$adm_reply['user_id'] = 0;

					$adm_reply['user_name'] = $_SESSION['adm_name'];

					$adm_reply['rec_module'] = $msgData['rec_module'];

					if ($adm_reply['rec_module'] == 'Order'){

						$adm_reply['rec_id'] = $msgData['rec_id'];

					}else{

						$adm_reply['rec_id'] = $id;

					}

					

					$adm_reply['score'] = 0;

					

					D("Message")->add($adm_reply);

				}

			}

			D("Message")->where("id=".$id)->setField("update_time",gmtTime());  //更新时间

			//成功提示

			$msg = '修改留言:'.$msgData['title'];

			$this->saveLog(1,$msgData['id'],$msg);

			$this->success (L('EDIT_SUCCESS'));

		} else {

			//错误提示

			$msg = '修改留言:'.$msgData['title'];

			$this->saveLog(0,$msgData['id'],$msg);

			$this->error (L('EDIT_FAILED'));

		}

	}



	//禁用与恢复操作

	public function forbid() {

		$name=$this->getActionName();

		$model = D ($name);

		$pk = $model->getPk ();

		$id = $_REQUEST [$pk];

		$msgData = M("Message")->getById($id);

		$condition = array ($pk => array ('in', $id ) );

		$list=$model->forbid ( $condition );

		if ($list!==false)
		{
			$msg = '禁用留言:'.$msgData['title'];

			$this->saveLog(1,$msgData['id'],$msg);

			$this->success ( L('FORBID_SUCCESS') );

		} else {

			$msg = '禁用留言:'.$msgData['title'];

			$this->saveLog(0,$msgData['id'],$msg);

			$this->error  (  L('FORBID_FAILED') );

		}

	}

	function resume() {

		//恢复指定记录

		$name=$this->getActionName();

		$model = D ($name);

		$pk = $model->getPk ();

		$id = $_GET [$pk];

		$msgData = M("Message")->getById($id);

		$condition = array ($pk => array ('in', $id ) );

		if (false !== $model->resume ( $condition )) {

			$msg = '恢复留言:'.$msgData['title'];

			$this->saveLog(1,$msgData['id'],$msg);

			$this->success ( L('RESUME_SUCCESS') );

		} else {

			$msg = '恢复留言:'.$msgData['title'];

			$this->saveLog(0,$msgData['id'],$msg);

			$this->error ( L('RESUME_FAILED') );

		}

	}

	

	

	public function foreverdelete() {

		//删除指定记录

		$name=$this->getActionName();

		$model = D ($name);

		if (! empty ( $model )) {

			$pk = $model->getPk ();

			$id = $_REQUEST [$pk];

			$ids = explode ( ',', $id );

			$names = '';
			
			$rec_ids = array();
			foreach($ids as $idd)
			{
				$message = M("Message")->where("id=".$idd)->field('title,rec_module,rec_id')->find();
				if(!empty($message['rec_module']) && intval($message['rec_id']) > 0)
					$rec_ids[$message['rec_module']][] = intval($message['rec_id']);
				
				$names .= ','.$message['title'];
			}
			
			if($names!='')
			{
				$names = substr($names,1);
			}

			if (isset ( $id ))
			{
				$condition = array ($pk => array ('in', explode ( ',', $id ) ) );
				if (false !== $model->where ( $condition )->delete ())
				{
					$this->updateRecInfo($rec_ids);
					//echo $model->getlastsql();

					$model->where(array ("pid" => array ('in', explode ( ',', $id ) ) ))->delete();  //删除相关的回复数据

					$msg = '删除留言:'.$names;

					$this->saveLog(1,0,$msg);

					$this->success (L('DEL_SUCCESS'));

				} else {

					$msg = '删除留言:'.$names;

					$this->saveLog(0,0,$msg);

					$this->error (L('DEL_FAILED'));

				}

			} else {

				$msg = '删除留言:'.$names;

				$this->saveLog(0,0,$msg);

				$this->error ( L('INVALID_OP') );

			}

		}

		$this->forward ();

	}

	private function updateRecInfo($rec_ids)
	{
		foreach($rec_ids as $module=>$ids)
		{
			if(count($ids) > 0)
			{
				$ids = array_unique($ids);
				if($module == 'Goods')
				{
					$condition = array ('id' => array('in',$ids));
					D('Goods')->where($condition)->setDec('post_count');
				}
				elseif($module == 'SiteMessage')
				{
					foreach($ids as $id)
					{
						$avg_score = D('Message')->where("status = 1 AND rec_id = $id AND rec_module = 'SiteMessage'")->avg('score');
						$avg_score = round($avg_score,1);
						if($avg_score == 0)
							$avg_score = 1;
						
						D('Site')->where('id = '.$id)->setField('score',$avg_score);
					}	
				}
			}
		}
	}

	public function getOrderSN($orderID)

	{

		return D("Order")->where("id = $orderID")->getField('sn');

	}

	

	public function autoAddUser(){

		$name=$this->getActionName();
		$model = M ( $name );		
		$msg_id = intval($_REQUEST['id']);
		$vo = $model->getById ($msg_id);
		$site_id = intval($_REQUEST['site_id']);

		if($site_id == 0)
		{
			$vo_site['name'] = trim($_REQUEST['title']);
			$vo_site['cate_id'] = intval($_REQUEST['rec_id']);
			$vo_site['url'] = trim($_REQUEST['content']);
			$vo_site['site_key'] = trim($_REQUEST['site_key']);
			$vo_site['status'] = 1;
			$vo_site['sort'] = D("Site")-> max("sort") + 1;
			$site_id = D("Site")->add($vo_site);
		}

		if(intval($_REQUEST['rule_type']) > 0 && $site_id > 0)
		{
			D("SiteRules")->where("id=$site_id")->delete();
			D("Site")->where("id=$site_id")->setField('rule_type',intval($_REQUEST['rule_type']));
			$rules = D("CollectRules")->where("id=".intval($_REQUEST['rule_type']))->getField("name,rule");
			$rules['collect_type'] = 2;
			foreach($rules as $key => $value)
			{
				if(!empty($value))
				{
					$site_rules = array();
					$site_rules['id'] = $site_id;
					$site_rules['name'] = $key;
					$site_rules['rule'] = $value;
					D("SiteRules")->add($site_rules);
				}
			}
		}
		
		if ($site_id > 0)
		{
			$vo_user['user_name'] = $_REQUEST['user_name'];
			$vo_user['email'] = $_REQUEST['user_email'];
			$vo_user['user_pwd'] = $vo['user_pwd'];
			$vo_user['user_pwd_confirm'] = $vo['user_pwd'];
			$vo_user['status'] = 1;
			$vo_user['tel'] = $_REQUEST['tel'];
			$vo_user['qq_msn'] = $_REQUEST['qq_msn'];
			$vo_user['create_time'] = gmtTime();
			$vo_user['update_time'] = gmtTime();
			$vo_user['group_id'] = '2';
			$vo_user['group_type'] = 'Site';
			$vo_user['rec_id'] = $site_id;
			
			$error = '';
			
			$code = fanweC('INTEGRATE_CODE');
			if (empty($code))
				$code = 'fanwe';
				
			if($code != 'fanwe')
			{
				$users = &init_users();
				$is_add = $users->add_user($vo_user['user_name'],$vo_user['user_pwd'],$vo_user['email']);
				if(!$is_add)
					$this->error(L(strtoupper($users->error)));
				else
				{
					$uc_arr = $users->get_profile_by_name($vo_user['user_name']);
					$vo_user['ucenter_id'] = $uc_arr['id'];
				}
			}
			else
			{
				$user_exist = D('user')->where("email = '$vo_user[email]' OR user_name = '$vo_user[user_name]'")->count();
				
				if($user_exist > 0)
					$this->error ('站点管理员 账号已经存在，无法添加！');
			}
			
			$vo_user['user_pwd'] = md5($vo['user_pwd']);
			$uid = D("User")->add($vo_user);
				
			if ($uid)
			{
				M('Site')->where('id='.$site_id)->setField('user_id',$uid);
				$model-> where("id = $msg_id")->setField("is_top",1);
				$this->success('添加站点管理员成功！');
			}
			else 
				$this->error ('已经成功添加站加，但添加站点管理员失败！');
		}
		else
		{
			$this->error ('添加站点失败，请先添加站点后再添加：站点管理员！');	
		}
	}

	public function getTypeSite($is_top)

	{

		if($is_top == 0)

			return "<span style='color:#00f'>未处理</span>";

		else

			return "<span style='color:#f00'>已处理</span>";

	}

}

?>